package com.wu.demo.farmework.config;

import com.wu.demo.common.Constants;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Swagger2的接口配置
 */
@Configuration
@EnableOpenApi //开启swagger
public class SwaggerConfig {
    //访问地址  http://localhost:1011/swagger-ui/index.html
    //knife4j  swagger-bootstrap-ui优化后地址 http://localhost:1011/doc.html
    /**
     * Swagger开关配置
     */
    @Value("${swagger.enable}")
    private boolean swaggerEnable;

    /**
     * 创建API
     */
    @Bean
    public Docket createRestApi() {
        List<RequestParameter> pars = new ArrayList<RequestParameter>();
        RequestParameterBuilder ticketPar = new RequestParameterBuilder();
        ticketPar.name(Constants.HEADER_TOKEN_KEY).description("登录校验")//name表示名称，description表示描述
                .in(ParameterType.HEADER)//自定义参数所在位置
                .required(false)//required表示是否必填
                .build();
        pars.add(ticketPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerEnable)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wu"))
                .paths(PathSelectors.any())
                .build()
                .globalRequestParameters(pars)
                .securitySchemes(Collections.singletonList(securitySchemes()) )
                .securityContexts(securityContexts())
                ;
    }

    /**
     * 安全模式，这里指定token通过Authorization头请求头传递
     */
    private ApiKey securitySchemes() {
        return new ApiKey(Constants.HEADER_TOKEN_KEY, Constants.HEADER_TOKEN_KEY, In.HEADER.name());
    }

    /**
     * 安全上下文
     */
    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts = new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.any())
                        .build());
        return securityContexts;
    }

    /**
     * 默认的安全上引用
     */
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences = new ArrayList<>();
        securityReferences.add(new SecurityReference(Constants.HEADER_TOKEN_KEY, authorizationScopes));
        return securityReferences;
    }

    /**
     * 添加摘要信息
     */
    private ApiInfo apiInfo() {
        // 用ApiInfoBuilder进行定制
        return new ApiInfoBuilder()
                // 设置标题
                .title("接口文档")
                // 描述
                .description("")
                // 作者信息
                .contact(new Contact("", null, null))
                // 版本
                .version("版本号:1.0.0" )
                .build();
    }
}
